package com.wms.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.ConfirmPackageDto;
import com.wms.dto.OutboundOrderPackageDto;
import com.wms.mapper.OutboundOrderDetailsMapper;
import com.wms.mapper.OutboundOrderMapper;
import com.wms.mapper.OutboundOrderPackageMapper;
import com.wms.mapper.OutboundOrderWeightsMapper;
import com.wms.pojo.*;
import com.wms.service.OutboundOrderDetailsService;
import com.wms.service.OutboundOrderPackageService;
import com.wms.service.OutboundOrderService;
import com.wms.service.OutboundOrderWeightsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class OutboundOrderPackageServiceImpl extends ServiceImpl<OutboundOrderPackageMapper, OutboundOrderPackage> implements OutboundOrderPackageService {
    private final OutboundOrderMapper orderMapper;
    private final OutboundOrderDetailsMapper detailsMapper;
    private final OutboundOrderWeightsMapper weightsMapper;

    @Override
    public PageResult queryPage(Integer page, Integer pageSize, Integer status, String packageCode, String customerName) {
        PageHelper.startPage(page,pageSize);
        List<OutboundOrderPackageDto> list = getBaseMapper().queryPage(status,packageCode,customerName);
        Page<OutboundOrderPackageDto> packageDtoPage = (Page<OutboundOrderPackageDto>) list;
        return new PageResult(packageDtoPage.getTotal(),packageDtoPage.getResult());
    }

    //确定打包
    @Transactional
    @Override
    public void updateByIdStatus(ConfirmPackageDto dto) {
        OutboundOrderPackage outboundOrderPackage = getById(dto.getId());
        OutboundOrderPackage orderPackage = OutboundOrderPackage.builder()
                .id(dto.getId())
                .updateTime(LocalDateTime.now())
                .packageEmpId(dto.getPackageEmpId())
                .status(2)
                .packageTime(dto.getPackageTime())
                .build();
        updateById(orderPackage);

        //修改发货单的状态
        OutboundOrderDetails details = detailsMapper.selectById(outboundOrderPackage.getOutboundOrderDetailId());
        //发货单
        OutboundOrder order = OutboundOrder.builder().id(details.getOutboundOrderId())
                .updateTime(LocalDateTime.now())
                .status(6)
                .build();
        orderMapper.updateById(order);

        // 生成8位数字随机数
        int eightDigitNumber = (int)(Math.random() * 90000000 + 10000000);
        String weightsCode = "weight"+eightDigitNumber;
        //添加称重数据
        OutboundOrderWeights orderWeights = OutboundOrderWeights.builder()
                .outboundOrderDetailId(details.getId())
                .status(1)
                .weightsCode(weightsCode)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        weightsMapper.insert(orderWeights);
    }
}
    